package com.bndg.blackjack.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.DashPathEffect
import android.graphics.Paint
import android.graphics.PathEffect
import android.graphics.Typeface
import android.util.AttributeSet
import android.view.View
import com.bndg.blackjack.R
import kotlin.math.min
/**
 * @author r
 * @date 2024/12/10
 * @description 筹码View
 */

class ChipView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
    private var ringColor: Int = Color.YELLOW
    private var chipColor: Int = Color.RED
    private var chipPaintMain: Paint? = null // 整体颜色
    private var textPaint: Paint? = null // 显示文字
    private var ringPaint: Paint? = null // 圆环
    private var dashPaint: Paint? = null // 虚线

    private var chipValue = "100" // 默认筹码值

    init {
        attrs?.let {
            val a = context?.obtainStyledAttributes(attrs, R.styleable.ChipView)
            chipValue = a?.getString(R.styleable.ChipView_chipValue) ?: chipValue

            chipColor = a?.getColor(R.styleable.ChipView_chipColor, Color.RED)!!
            ringColor = a?.getColor(R.styleable.ChipView_ringColor, Color.YELLOW)!!
            a?.recycle()
        }
        init()
    }

    private fun init() {
        // 设置筹码主颜色
        chipPaintMain = Paint()
        chipPaintMain!!.color = chipColor!!
        chipPaintMain!!.style = Paint.Style.FILL
        chipPaintMain!!.isAntiAlias = true // 启用抗锯齿

        // 设置文字颜色和大小
        textPaint = Paint()
        textPaint!!.color = Color.WHITE
        textPaint!!.isAntiAlias = true // 启用抗锯齿
        // 设置为加粗和斜体
        textPaint!!.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD_ITALIC))
        textPaint!!.textSize = 45f
        textPaint!!.textAlign = Paint.Align.CENTER

        // 设置圆环画笔
        ringPaint = Paint()
        ringPaint!!.color = ringColor!!
        ringPaint!!.style = Paint.Style.STROKE
        ringPaint!!.strokeWidth = 20f
        ringPaint!!.isAntiAlias = true // 启用抗锯齿

        // 设置虚线画笔
        dashPaint = Paint()
        dashPaint!!.color = Color.DKGRAY
        dashPaint!!.style = Paint.Style.STROKE
        dashPaint!!.strokeWidth = 10f
        dashPaint!!.isAntiAlias = true // 启用抗锯齿
        val effects: PathEffect = DashPathEffect(floatArrayOf(20f, 20f), 0f)
        dashPaint!!.setPathEffect(effects)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val size = MeasureSpec.getSize(widthMeasureSpec) // 获取测量的宽度
        setMeasuredDimension(size, size) // 使宽度和高度相等
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val centerX = width / 2
        val centerY = height / 2
        val radius = (min(centerX.toDouble(), centerY.toDouble()) - 40).toFloat() // 半径

        // 绘制黄颜色筹码
        canvas.drawCircle(centerX.toFloat(), centerY.toFloat(), radius + 40, chipPaintMain!!)

        // 绘制外圆环
        canvas.drawCircle(centerX.toFloat(), centerY.toFloat(), radius + 20, ringPaint!!)

        // 绘制虚线
        canvas.drawCircle(centerX.toFloat(), centerY.toFloat(), radius + 20, dashPaint!!)

        // 绘制文本 由于斜体会显得文字不在中心 所以x往左偏移一点
        canvas.drawText(
            chipValue,
            centerX - textPaint!!.textSize / 6,
            centerY + textPaint!!.textSize / 3,
            textPaint!!
        )
    }

    fun setChipValue(value: String) {
        this.chipValue = value
        invalidate() // 刷新视图
    }

    fun getChipValue(): String {
        return chipValue
    }

    fun setChipColor(color: Int) {
        chipColor = color
        chipPaintMain?.color = chipColor!!
        invalidate()
    }

    fun getChipColor(): Int {
        return chipColor!!
    }

    fun setRingColor(color: Int) {
        ringColor = color
        ringPaint?.color = ringColor!!
        invalidate()
    }

    fun getRingColor(): Int {
        return ringColor!!
    }
}